home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacTreeCellRenderer.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  225 lines

  1. /*
  2.  * @(#)MacTreeCellRenderer.java    1.6 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.plaf.*;
  25. import com.sun.java.swing.tree.*;
  26. import com.sun.java.swing.plaf.basic.BasicTreeCellRenderer;
  27. import com.sun.java.accessibility.*;
  28. import java.awt.*;
  29. import java.awt.event.*;
  30. import java.beans.*;
  31. import java.io.*;
  32. import java.util.*;
  33.  
  34. /**
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version @(#)MacTreeCellRenderer.java    1.0 11/24/97
  44.  * @author Symantec
  45.  */
  46. public class MacTreeCellRenderer extends BasicTreeCellRenderer
  47. {
  48.     public MacTreeCellRenderer()
  49.     {
  50.         setSelectedIcon(defaultSelectedIcon);
  51.         setSelectedLeafIcon(defaultSelectedLeafIcon);
  52.         setFont(UIManager.getFont("Tree.font"));
  53.         setIconTextGap(getIconTextGap() + 1);
  54.     }
  55.  
  56.     /**
  57.      * Returns the graphic image (glyph, icon) that the node displays when selected.
  58.      *
  59.      * @return an Icon
  60.      * @see #setSelectedIcon
  61.      */
  62.     public Icon getSelectedIcon()
  63.     {
  64.         return selectedIcon;
  65.     }
  66.  
  67.     /**
  68.      * Defines the icon this component will display when selected.  If
  69.      * the value of icon is null, the normal icon is displayed.
  70.      * <p>
  71.      * This is a JavaBeans bound property.  
  72.      * 
  73.      * @see #getSelectedIcon
  74.      */
  75.     public void setSelectedIcon(Icon icon)
  76.     {
  77.         Icon oldValue = icon;
  78.         selectedIcon = icon;
  79.         firePropertyChange("selectedIcon", oldValue, selectedIcon);
  80.  
  81.         AccessibleContext ac = getAccessibleContext();
  82.         if ((ac != null) && (oldValue != selectedIcon))
  83.         {
  84.             ac.firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, oldValue, selectedIcon);
  85.         }
  86.  
  87.         repaint(10);
  88.     }
  89.  
  90.     /**
  91.      * Returns the graphic image (glyph, icon) that a leaf node displays when selected.
  92.      *
  93.      * @return an Icon
  94.      * @see #setSelectedLeafIcon
  95.      */
  96.     public Icon getSelectedLeafIcon()
  97.     {
  98.         return selectedLeafIcon;
  99.     }
  100.  
  101.     /**
  102.      * Defines the icon this component will display when a leaf is selected.  If
  103.      * the value of icon is null, the normal icon is displayed.
  104.      * <p>
  105.      * This is a JavaBeans bound property.  
  106.      * 
  107.      * @see #getSelectedLeafIcon
  108.      */
  109.     public void setSelectedLeafIcon(Icon icon)
  110.     {
  111.         Icon oldValue = icon;
  112.         selectedLeafIcon = icon;
  113.         firePropertyChange("selectedLeafIcon", oldValue, selectedLeafIcon);
  114.  
  115.         AccessibleContext ac = getAccessibleContext();
  116.         if ((ac != null) && (oldValue != selectedLeafIcon))
  117.         {
  118.             ac.firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, oldValue, selectedLeafIcon);
  119.         }
  120.  
  121.         repaint(10);
  122.     }
  123.  
  124.     public Dimension getPreferredSize()
  125.     {
  126.         Dimension retDimension = super.getPreferredSize();
  127.     
  128.         if(retDimension != null)
  129.             retDimension = new Dimension(retDimension.width, retDimension.height + 2);
  130.             
  131.         return retDimension;
  132.     }
  133.  
  134.     /**
  135.       * Configures the renderer based on the passed in components.
  136.       * The value is set from messaging value with toString().
  137.       * The foreground color is set based on the selection and the icon
  138.       * is set based on on leaf and expanded.
  139.       */
  140.     public Component getTreeCellRendererComponent(JTree tree, Object value,
  141.                                                       boolean sel,
  142.                                                       boolean expanded,
  143.                                                       boolean leaf, int row,
  144.                                                       boolean hasFocus)
  145.     {
  146.         String stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
  147.         
  148.         setText(stringValue);
  149.         selected = sel;
  150.  
  151.         if (leaf)
  152.         {
  153.             if (selected)
  154.             {
  155.                 setIcon(getSelectedLeafIcon());
  156.                 return this;
  157.             }
  158.             else
  159.             {
  160.                 setIcon(getLeafIcon());
  161.                 return this;
  162.             }
  163.         }
  164.         
  165.         if (selected)
  166.         {
  167.             Icon temp = getSelectedIcon();
  168.             if(temp != null)
  169.             {
  170.                 setIcon(temp);
  171.                 return this;
  172.             }
  173.         }
  174.         
  175.         if (expanded)
  176.         {
  177.             setIcon(getOpenIcon());
  178.         }
  179.         else
  180.         {
  181.             setIcon(getClosedIcon());
  182.         }
  183.         
  184.         return this;
  185.     }
  186.  
  187.     public void paint(java.awt.Graphics g)
  188.     {
  189.         g.setFont(getFont());
  190.         
  191.            FontMetrics    fm                = g.getFontMetrics();
  192.         int            stringWidth        = fm.stringWidth(getText());
  193.         int            stringHeight    = fm.getAscent();// + fm.getLeading();
  194.         int            xOffset            = 0;
  195.         int            yOffset            = 0;
  196.         Icon        currentIcon        = null;
  197.         
  198.            currentIcon = getIcon();
  199.  
  200.         if(currentIcon != null)
  201.         {
  202.             xOffset = currentIcon.getIconWidth() + getIconTextGap();
  203.             currentIcon.paintIcon(this, g, 0, 0);
  204.         }
  205.         
  206.         yOffset = ((getHeight() - 1 - stringHeight) >> 1) - 1;
  207.  
  208.         if(selected)
  209.         {
  210.                g.setColor(textSelectionColor);
  211.               g.fillRect(xOffset - 1, yOffset, stringWidth + 2, stringHeight + fm.getDescent());
  212.         }
  213.  
  214.         g.setColor(getTextNonSelectionColor());
  215.            g.drawString(getText(), xOffset, stringHeight - 1 + yOffset);
  216.            
  217.    }
  218.  
  219.     transient protected static Icon defaultSelectedIcon        = UIManager.getIcon("Tree.selectedIcon");
  220.     transient protected static Icon defaultSelectedLeafIcon = UIManager.getIcon("Tree.selectedLeafIcon");
  221.  
  222.     transient protected Icon selectedIcon        = null;
  223.     transient protected Icon selectedLeafIcon    = null;
  224. }
  225.